home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
MacHack 1997
/
MacHack 1997.toast
/
Hacks
/
Hacks ’93
/
sort
/
Source
/
sortMenus.c
< prev
next >
Wrap
Text File
|
1993-06-17
|
12KB
|
477 lines
/*****
* sortMenus.c
*
*
*****/
#include <Menus.h>
#include <Windows.h>
#include <ToolUtils.h>
#include <Desk.h>
#include <Resources.h>
#include <Memory.h>
#include <Packages.h>
#include <Dialogs.h>
#include "sortWindow.h"
#include "sortMenus.h"
#include "sortdata.h"
#include "sort.h"
#include "acur.h"
#include "notif.h"
#define CONFIG_DATAID 128
#define CONFIG_DISPID 129
#define DONEALERTID 129
#define CANCELALERTID 130
#define ABOUTALERTID 131
extern WindowPtr sortWindow;
extern Boolean done;
#define NUMDATAITEM 4
#define MINDATAITEM 6
#define MAXDATAITEM 8
#define OK_USERITEM 9
#define HILITECOMPITEM 3
#define HILITESWAPITEM 4
#define HILITEDELITEM 5
#define UNHILITEDELITEM 6
MenuHandle appleMenu, fileMenu, editMenu, sortMenu, configureMenu, DebugMenu;
short stop_sort;
Str255 sort_name;
enum {
appleID = 1,
fileID,
editID,
sortID,
configureID,
DebuggerID
};
enum {
openItem = 1,
closeItem,
quitItem = 4
};
enum {
about_Item = 1
};
enum {
configdata_Item = 1,
configdisp_Item = 2
};
enum {
stop_sortItem = 1
};
enum {
gointoDebugger = 1
};
/****
* SetUpMenus()
*
* Set up the menus. Normally, we’d use a resource file, but
* for this example we’ll supply “hardwired” strings.
*
****/
void SetUpMenus(void)
{
stop_sort = false;
InsertMenu(appleMenu = NewMenu(appleID, "\p\024"), 0);
InsertMenu(fileMenu = NewMenu(fileID, "\pFile"), 0);
InsertMenu(editMenu = NewMenu(editID, "\pEdit"), 0);
InsertMenu(sortMenu = NewMenu(sortID, "\pSort"), 0);
InsertMenu(configureMenu = NewMenu(configureID, "\pConfigure"), 0);
InsertMenu(DebugMenu = NewMenu(DebuggerID, "\pDebug"), 0);
DrawMenuBar();
AppendMenu(appleMenu, "\pAbout Sort Demo…;(-");
AddResMenu(appleMenu, 'DRVR');
AppendMenu(fileMenu, "\pOpen/O;Close/W;(-;Quit/Q");
AppendMenu(editMenu, "\pUndo/Z;(-;Cut/X;Copy/C;Paste/V;Clear");
AppendMenu(sortMenu, "\pStop/.;(-");
AddResMenu(sortMenu, 'SORT');
AppendMenu(configureMenu, "\pConfigure Data…;Configure Display…");
AppendMenu(DebugMenu, "\pGoInto Debugger/D");
}
/* end SetUpMenus */
/****
* AdjustMenus()
*
* Enable or disable the items in the Edit menu if a DA window
* comes up or goes away. Our application doesn't do anything with
* the Edit menu.
*
****/
int enable (MenuHandle menu, short item, short ok);
void AdjustMenus(void)
{
register WindowPeek wp = (WindowPeek) FrontWindow();
short kind = wp ? wp->windowKind : 0;
short num, count;
Boolean DA = kind < 0;
Boolean invalid;
enable(editMenu, 1, DA);
enable(editMenu, 3, DA);
enable(editMenu, 4, DA);
enable(editMenu, 5, DA);
enable(editMenu, 6, DA);
invalid = (((WindowPeek) sortWindow)->visible == true) ||
(mindataval < 1) ||
(maxdataval > (long)windowBounds.right - 10) ||
(height < 1L);
enable(fileMenu, openItem, invalid == false);
enable(fileMenu, closeItem, DA || ((WindowPeek) sortWindow)->visible);
num = CountMItems(sortMenu);
enable(sortMenu, stop_sortItem, sorting);
for (count = stop_sortItem + 1; count <= num; count++) {
enable(sortMenu, count, !sorting);
}
enable(configureMenu, configdata_Item, !sorting);
enable(configureMenu, configdisp_Item, true);
enable(DebugMenu, configdisp_Item, true);
}
static
enable(MenuHandle menu, short item, short ok)
{
if (ok)
EnableItem(menu, item);
else
DisableItem(menu, item);
}
/*****
* HandleMenu(mSelect)
*
* Handle the menu selection. mSelect is what MenuSelect() and
* MenuKey() return: the high word is the menu ID, the low word
* is the menu item
*
*****/
void HandleMenu (long mSelect)
{
int menuID = HiWord(mSelect);
short menuItem = LoWord(mSelect);
short ihit;
Str255 name,excs,cmps, ticks;
GrafPtr savePort;
WindowPeek frontWindow;
DialogPtr dialogwindow;
Handle h;
sortptr srt;
long starrttime, finishtime;
switch (menuID)
{
case appleID:
if (menuItem != about_Item) {
GetPort(&savePort);
GetItem(appleMenu, menuItem, name);
OpenDeskAcc(name);
SetPort(savePort);
}
else {
Alert(ABOUTALERTID, NULL);
}
break;
case sortID:
sorting = true;
switch (menuItem)
{
case stop_sortItem:
stop_sort = true;
return;
default:
HiliteMenu(0);
GetItem(sortMenu, menuItem, sort_name);
numexchanges = 0;
numcompares = 0;
if (init_data(numdataitems) != noErr) {
numdataitems = 2;
height=((((windowBounds.bottom - windowBounds.top) - numdataitems) -5)) / (numdataitems + 1);
init_data(numdataitems);
}
h = GetNamedResource('SORT', sort_name);
LoadResource(h);
HLock(h);
HNoPurge(h);
starrttime = TickCount();
srt = (sortptr)(*h);
(*srt)(numdataitems, sortdata, swap, compare, &stop_sort);
finishtime = TickCount();
ReleaseResource(h);
break;
}
SetCursor(&qd.arrow);
if (gInBackground) {
wait_Foreground(1);
}
NumToString(finishtime - starrttime, (StringPtr)&ticks);
NumToString((long)numdataitems, (StringPtr) &name);
NumToString(numcompares, (StringPtr) &cmps);
NumToString(numexchanges, (StringPtr) &excs);
ParamText((StringPtr)&name, (StringPtr)&cmps, (StringPtr)excs, (StringPtr)&ticks);
if (!stop_sort) {
data_sorted = true;
Alert(DONEALERTID, NULL);
}
else {
Alert(CANCELALERTID, NULL);
}
stop_sort = false;
sorting = false;
break;
case fileID:
switch (menuItem)
{
case openItem:
ShowWindow(sortWindow);
SelectWindow(sortWindow);
break;
case closeItem:
if ((frontWindow = (WindowPeek) FrontWindow()) == 0L)
break;
if (frontWindow->windowKind < 0)
CloseDeskAcc(frontWindow->windowKind);
else if (frontWindow = (WindowPeek) sortWindow)
HideWindow(sortWindow);
break;
case quitItem:
DisposAcur();
done = true;
break;
}
break;
case configureID:
{
Str255 numString;
Rect box;
Handle hItem;
short itemType;
long newnumdata, newmindata, newmaxdata, newheight,
newhilitedelay, newunhilitedelay;
Boolean invalid;
switch (menuItem) {
case configdata_Item:
dialogwindow = GetNewDialog(CONFIG_DATAID, nil, (WindowPtr) -1);
// Fix ME GetDItem (dialogwindow, OK_USERITEM, &itemType, &hItem, &box);
// SetDItem (dialogwindow, OK_USERITEM, itemType, (Handle)FrameUserProc , &box);
GetDItem (dialogwindow, NUMDATAITEM, &itemType, &hItem, &box);
NumToString (numdataitems, (StringPtr)&numString);
SetIText (hItem, (StringPtr)&numString);
GetDItem (dialogwindow, MINDATAITEM, &itemType, &hItem, &box);
NumToString (mindataval, (StringPtr)&numString);
SetIText (hItem, (StringPtr)&numString);
GetDItem (dialogwindow, MAXDATAITEM, &itemType, &hItem, &box);
NumToString (maxdataval, (StringPtr)&numString);
SetIText (hItem, (StringPtr)&numString);
do {
invalid = false;
do {
ModalDialog (nil, &ihit);
} while ((ihit != ok) && (ihit != cancel));
if (ihit == ok) {
GetDItem (dialogwindow, NUMDATAITEM, &itemType, &hItem, &box);
GetIText (hItem, (StringPtr)&numString);
StringToNum ((StringPtr)&numString, &newnumdata);
if (newnumdata < 2L) {
SysBeep(1);
invalid = true;
continue;
}
GetDItem (dialogwindow, MINDATAITEM, &itemType, &hItem, &box);
GetIText (hItem, (StringPtr)&numString);
StringToNum ((StringPtr)&numString, &newmindata);
if (((WindowPeek) sortWindow)->visible) {
if ((newmindata < 1) || (newmindata > (long)windowBounds.right - 10)) {
SysBeep(1);
invalid = true;
continue;
}
}
GetDItem (dialogwindow, MAXDATAITEM, &itemType, &hItem, &box);
GetIText (hItem, (StringPtr)&numString);
StringToNum ((StringPtr)&numString, &newmaxdata);
if (((WindowPeek) sortWindow)->visible) {
if ((newmaxdata < newmindata) || (newmaxdata > (long)windowBounds.right - 10)) {
SysBeep(1);
invalid = true;
continue;
}
}
newheight=((((windowBounds.bottom - windowBounds.top) - newnumdata) -5)) / (newnumdata + 1);
if (((WindowPeek) sortWindow)->visible) {
if ((newheight < 1L)) {
SysBeep(1);
invalid = true;
continue;
}
}
else {
if (newmaxdata < newmindata) {
SysBeep(1);
invalid = true;
continue;
}
invalid = (newmindata < (long)1) ||
(newmaxdata > (long)windowBounds.right - 10) ||
(newheight < 1L);
enable(fileMenu, openItem, invalid == false);
invalid = false;
}
SetPort(sortWindow);
EraseRect(&sortWindow->portRect);
numdataitems = newnumdata;
mindataval = newmindata;
maxdataval = newmaxdata;
height=((((windowBounds.bottom - windowBounds.top) - numdataitems) -5)) / (numdataitems + 1);
data_sorted = true;
if (init_data(numdataitems) != noErr) {
numdataitems = 2;
height=((((windowBounds.bottom - windowBounds.top) - numdataitems) -5)) / (numdataitems + 1);
init_data(numdataitems);
}
}
} while (invalid);
DisposeDialog(dialogwindow);
break;
case configdisp_Item:
dialogwindow = GetNewDialog(CONFIG_DISPID, nil, (WindowPtr) -1);
// FIX ME GetDItem (dialogwindow, OK_USERITEM, &itemType, &hItem, &box);
// SetDItem (dialogwindow, OK_USERITEM, itemType, (Handle)FrameUserProc , &box);
GetDItem (dialogwindow, HILITEDELITEM, &itemType, &hItem, &box);
NumToString (hilitedelay, (StringPtr)&numString);
SetIText (hItem, (StringPtr)&numString);
GetDItem (dialogwindow, UNHILITEDELITEM, &itemType, &hItem, &box);
NumToString (unhilitedelay, (StringPtr)&numString);
SetIText (hItem, (StringPtr)&numString);
GetDItem (dialogwindow, HILITESWAPITEM, &itemType, &hItem, &box);
SetCtlValue((ControlHandle)hItem, (short)hilite_exchange);
GetDItem (dialogwindow, HILITECOMPITEM, &itemType, &hItem, &box);
SetCtlValue((ControlHandle)hItem, (short)hilite_compare);
do {
invalid = false;
do {
ModalDialog (nil, &ihit);
if ((ihit == HILITESWAPITEM) || (ihit == HILITECOMPITEM)) {
GetDItem (dialogwindow, ihit, &itemType, &hItem, &box);
SetCtlValue((ControlHandle)hItem, 1 - GetCtlValue((ControlHandle)hItem));
}
} while ((ihit != ok) && (ihit != cancel));
if (ihit == ok) {
GetDItem (dialogwindow, HILITEDELITEM, &itemType, &hItem, &box);
GetIText (hItem, (StringPtr)&numString);
StringToNum ((StringPtr)&numString, &newhilitedelay);
GetDItem (dialogwindow, UNHILITEDELITEM, &itemType, &hItem, &box);
GetIText (hItem, (StringPtr)&numString);
StringToNum ((StringPtr)&numString, &newunhilitedelay);
if ((newhilitedelay < 0L) || (newunhilitedelay < 0L)) {
SysBeep(1);
invalid = true;
continue;
}
hilitedelay = newhilitedelay;
unhilitedelay = newunhilitedelay;
GetDItem (dialogwindow, HILITESWAPITEM, &itemType, &hItem, &box);
hilite_exchange = (Boolean) GetCtlValue((ControlHandle)hItem);
GetDItem (dialogwindow, HILITECOMPITEM, &itemType, &hItem, &box);
hilite_compare = (Boolean) GetCtlValue((ControlHandle)hItem);
DrawSort(true);
}
} while(invalid);
DisposeDialog(dialogwindow);
break;
}
}
break;
case editID:
if (!SystemEdit(menuItem-1))
SysBeep(5);
break;
case DebuggerID:
Debugger();
break;
}
HiliteMenu(0);
}
/* end HandleMenu */